package cn.wolfcode.Controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.DepartmentService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    private DepartmentService departmentService;
    @Autowired
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }




//    @RequestMapping("/list")
//    public String listAll(Model model){
//        List<Department> departments = departmentMapper.selectAll();
//        model.addAttribute("departments",departments);
//        return "department/list";
//    }

    @RequirePermission(name = "查看部门",experssion = "department:list")
    @RequestMapping("/list")
    public String listAll(Model model,QueryObject queryObject){
        System.out.println(queryObject);
        PageInfo<Department> pageInfo = departmentService.listPage(queryObject);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }

    @RequirePermission(name = "部门权删除",experssion = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            departmentService.deleteByPrimaryKey(id);
        }
        return "redirect:/department/list";
    }

//    @RequestMapping("/input")
//    public String input(Long id,Model model){
//        if (id != null){
//            Department department = departmentService.selectByPrimaryKey(id);
//            model.addAttribute("department", department);
//        }
//        return "department/input";
//    }
@RequirePermission(name = "部门权限新增/编辑",experssion = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department,Model model){
        if (department.getId() != null){
            departmentService.updateByPrimaryKey(department);
        }else {
            departmentService.insert(department);
        }
        return "redirect:/department/list";
    }


}

